import UIKit

class RadarView: UIView {
    
    var radarData: [(angle: CGFloat, distance: CGFloat)] = [] {
        didSet {
            setNeedsDisplay()
        }
    }
    
    private let scanDuration: TimeInterval = 3.0 // 扫描一圈所需的时间
    private var currentScanAngle: CGFloat = 190
    private var pointsToDraw: [(angle: CGFloat, distance: CGFloat)] = []
    private var displayLink: CADisplayLink?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white;
        setupAnimation()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupAnimation()
    }
    
    private func setupAnimation() {
        displayLink = CADisplayLink(target: self, selector: #selector(updateScan))
        displayLink?.add(to: .main, forMode: .common)
    }
    
    @objc private func updateScan() {
        // 计算每帧需要增加的角度
        let angleIncrement = (180 / CGFloat(scanDuration)) * CGFloat(displayLink?.duration ?? 1/60)
        
        currentScanAngle += angleIncrement
        
        if currentScanAngle > 350 {
            currentScanAngle = 190
            pointsToDraw.removeAll()
        }
        
        // 更新需要绘制的点
        pointsToDraw = radarData.filter { $0.angle <= currentScanAngle }
        
        setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        
        let width = rect.width
        let height = rect.height
        
        let center = CGPoint(x: width / 2, y: height/2)
        let radius: CGFloat = min(width, height) / 2
        // 绘制固定的扇形边界
          let startAngleForBoundary = 0 * CGFloat.pi / 180
          let endAngleForBoundary = 180 * CGFloat.pi / 180
          
        context.setStrokeColor(UIColor.white.cgColor)
          context.setLineWidth(2.0)
          context.move(to: center)
          context.addArc(center: center, radius: radius, startAngle: startAngleForBoundary, endAngle: endAngleForBoundary, clockwise: false)
          context.closePath()
          context.strokePath()

        // 绘制扫描扇区
//        let startAngle = 0 * CGFloat.pi / 180
//        let endAngle = currentScanAngle * CGFloat.pi / 180
        
//        context.setFillColor(UIColor.blue.withAlphaComponent(0.2).cgColor)
//        context.move(to: center)
//        context.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
//        context.closePath()
//        context.fillPath()
        // 清除背景以便重新绘制
             context.clear(rect)
        // 根据当前扫描角度绘制一条直线
          let xEnd = center.x + radius * cos(currentScanAngle * CGFloat.pi / 180)
          let yEnd = center.y + radius * sin(currentScanAngle * CGFloat.pi / 180)
          
          context.setStrokeColor(UIColor.white.cgColor)
          context.setLineWidth(2.0)
          context.move(to: center)
          context.addLine(to: CGPoint(x: xEnd, y: yEnd))
          context.strokePath()
        
        // 绘制圆点表示距离
        for data in pointsToDraw {
            let x = center.x - data.distance * cos(data.angle * CGFloat.pi / 180)
            let y = center.y - data.distance * sin(data.angle * CGFloat.pi / 180)
            
            context.setFillColor(UIColor.green.cgColor)
            context.fillEllipse(in: CGRect(x: x - 5, y: y - 5, width: 10, height: 10))
        }
        
        // 绘制中心圆点
        context.setFillColor(UIColor.red.cgColor)
        context.fillEllipse(in: CGRect(x: center.x - 5, y: center.y - 5, width: 10, height: 10))
    }
    
    deinit {
        displayLink?.invalidate()
    }
}
